import {createRouter, createWebHistory} from 'vue-router'
import {ref} from "vue"
import VueCookies from 'vue-cookies'


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('@/views/Login')
  },
  {
    path: '/register',
    name: '注册',
    component: () => import('@/views/Register')
  },
  {
    path: '/resetPassword',
    name: '重置密码',
    component: () => import('@/views/resetPassword')
  },

  {
    path: '/home',
    name: '主页',
    component: () => import('@/views/home/Index'),
  },
  {
    path: '/documents/editing',
    name: '文档编辑',
    component: () => import('@/views/home/DocumentsEditing'),
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  let userInfo = VueCookies.get("userInfo");

  if (to.path === '/' || to.path === '/register' || to.path === '/login' || to.path === '/resetPassword') {
    if (to.path === '/login') {
      if (userInfo != null) {
        router.push("/home");
      }
    }
    next();
  } else {
    if(to.path === '/qqLogin'){
      next();
    }else {
      if (userInfo == null) {
        router.push("/login");
      }
      next();
    }
  }
})


export default router
